<template>
  <el-dialog
    v-model="showModel"
    width="350"
    :align-center="true"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    title="设置密码"
  >
    <div class="bind__phone">
      <form-list
        ref="refForm"
        :label="['phone', 'code', 'pwd']"
        :disbale-label="['phone']"
        :strong-password="true"
        :default-value="{ phone: user.userInfo.PhoneNumber }"
      />
      <div class="actions">
        <el-button
          type="primary"
          class="main-btn"
          size="large"
          @click="handleConfirm"
        >
          确定
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup name="SettingPwd">
import { ref } from "vue";
import formList, { type API } from "@/components/phone-form/form.vue";
import { updatePwd } from "@/api/login";
import PsMessage from "@/components/message/index";
import { userAppStore } from "@/stores/user";
import { getPwdMD5Str } from "@/utils";
// import { getXzUserInfo } from "@/utils/xz-browser";

export interface SettingPwdApi {
  open: () => void;
}
const user = userAppStore();
const showModel = ref(false);
const refForm = ref<API>();

const open = async (type: string) => {
  showModel.value = true;
};

const handleConfirm = async () => {
  const form = await refForm.value?.validate();
  if (!form) return;
  const uid = user.userInfo.user_id;
  const params: any = {
    type: "phone",
    phoneNumber: form.phone,
    oldPWD: "",
    pwd: getPwdMD5Str(form.pwd),
    code: form.code,
    codeID: form.phone,
    uid: Number(uid)
  };

  await updatePwd(params);
  PsMessage("设置密码成功");
  showModel.value = false;
  user.getUserInfo();
};

defineExpose({
  open
});
</script>

<style lang="scss" scoped>
.bind__phone {
  padding: 0 10px;
  color: var(--el-text-color-primary);
}
h2 {
  font-size: 16px;
  font-weight: bold;
  text-align: center;
  padding-bottom: 20px;
}
.main-btn {
  width: 100%;
  margin: 20px 0 10px 0;
}
</style>
